---
title: "COVID-19"
output:
flexdashboard::flex_dashboard:
logo: rbmv_curve.png
orientation: rows
source_code: embed
theme: flatly
---
```{r setup, include=FALSE}
library(flexdashboard)
```
```{r, include=FALSE}
# Packages
library(tidyverse)
library(lubridate)
library(rvest)
library(sf)
library(leaflet)
library(plotly)
library(rbmv)
# Functions
`%<>%` <- magrittr::`%<>%`
# API key
mapbox <- read_lines(".mapbox-key")
# Get the data + tidy
dph_covid19_page <- read_html("http://www.publichealth.lacounty.gov/media/Coronavirus/locations.htm")
dph_covid19_tbls <- dph_covid19_page %>%
html_nodes("table") %>%
html_table()
dph_covid19_messy <- dph_covid19_tbls[[1]] %>%
repair_names() %>%
janitor::clean_names() %>%
rename(locations = x1, total_cases = x2) %>%
select(-x3)
dph_covid19_totals <- dph_covid19_messy %>%
slice(2:11)
dph_covid19_gender <- dph_covid19_messy %>% slice(20:24) %>%
rename(gender = locations) %>%
mutate(total_cases = parse_number(total_cases))
lac_total_cases <- parse_number(dph_covid19_totals$total_cases[[1]])
lac_total_deaths <- parse_number(dph_covid19_totals$total_cases[[6]])
dph_covid19_totals %>%
mutate(locations = str_remove_all(locations, "- ")) %>%
filter(locations %in% c("Long Beach", "Pasadena")) %>%
slice(1:2) %>%
mutate(locations = str_glue("City of {locations}") %>% parse_character(),
total_cases = parse_number(total_cases)) -> lb_pass
dph_covid19 <- dph_covid19_messy %>% slice(30:365) %>%
mutate(
locations = str_remove_all(locations, regex("\\*")),
total_cases = parse_number(total_cases, na = "--"),
locations = case_when(
str_detect(locations, "San Francisquito") ~ "Unincorporated - San Francisquito Canyon/Bouquet Canyon",
TRUE ~ locations)) %>%
filter(!locations == "Los Angeles") %>%
bind_rows(., lb_pass)
captions <- dph_covid19_page %>%
html_nodes("caption") %>%
html_text()
str_remove_all(captions[1], "\\r|\\n|\\t") %>%
str_split_fixed(., "\\*", 2) -> page_updated
page_updated <- str_split_fixed(page_updated[1], " ", 4)[4]
page_updated <- str_glue("{page_updated}/20")
page_updated <- parse_date(page_updated, "%m/%d/%y")
# uncomment line below if the 'caption' html is not updated on DPH website (it's usually not updated)
page_updated <- Sys.Date()
page_updated_string <- str_glue("{wday(page_updated, label = T)} {month(page_updated, label = T)} {day(page_updated)}, {year(page_updated)}")
gis <- st_read(
dsn = "data/gis/geo_export_871186bb-3266-4a1c-a0f6-8cda86b55d55.shp",
layer = "geo_export_871186bb-3266-4a1c-a0f6-8cda86b55d55",
quiet = T
)
gis %<>% st_transform(crs = 4326)
lac_covid19 <- gis %>%
left_join(., dph_covid19, by = c("label" = "locations")) %>%
mutate(total_cases = if_else(is.na(total_cases), 0, total_cases))
covid19_cases <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv")
covid19_deaths <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_US.csv")
make_link <- function(date) {
date_format <- format(date, format = "%m-%d-%Y")
link <- str_glue("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/{date_format}.csv")
return(link)
}
test_data <- function(date) {
link <- make_link(date)
status <- httr::http_status(httr::GET(link))
return(status$category)
}
get_world_data <- function(date) {
if (test_data(date) == "Success") {
return(make_link(date))
} else if (test_data(date) == "Client error") {
return(make_link(date - 1))
}
}
link <- get_world_data(today())
world <- read_csv(link)
world %<>% rename(long = Long_, lat = Lat)
covid19_time_series_cases <- covid19_cases %>%
filter(Admin2 == "Los Angeles") %>%
gather("date", "cases", 12:ncol(covid19_cases)) %>%
select(Admin2, date, cases) %>%
rename(county = Admin2) %>%
mutate(date = parse_date(date, "%m/%d/%y"),
case_type = "New Confirmed Case") %>%
bind_rows(
tribble(
~county, ~date, ~cases, ~case_type,
"Los Angeles", page_updated, lac_total_cases, "New Confirmed Case")) %>%
distinct()
covid19_time_series_deaths <- covid19_deaths %>%
filter(Admin2 == "Los Angeles") %>%
gather("date", "cases", 13:ncol(covid19_deaths)) %>%
select(Admin2, date, cases) %>%
rename(county = Admin2) %>%
mutate(date = parse_date(date, "%m/%d/%y"),
case_type = "Death") %>%
bind_rows(
tribble(
~county, ~date, ~cases, ~case_type,
"Los Angeles", page_updated, lac_total_deaths, "Death")) %>%
distinct()
covid19_time_series <- bind_rows(covid19_time_series_cases, covid19_time_series_deaths)
outbreaks <- c(
"New York City, New York, US", "Nassau, New York, US", "Suffolk, New York, US",
"Westchester, New York, US", "Cook, Illinois, US", "Wayne, Michigan, US",
"Bergen, New Jersey, US", "Hudson, New Jersey, US",
"Essex, New Jersey, US")
covid19_cases_top10_us <- covid19_cases %>%
filter(Combined_Key %in% outbreaks) %>%
gather("date", "cases", 12:ncol(covid19_cases)) %>%
select(Combined_Key, date, cases) %>%
rename(location = Combined_Key) %>%
mutate(date = parse_date(date, "%m/%d/%y"),
case_type = "New Confirmed Case") %>%
distinct() %>%
arrange(date, desc(cases))
light_blue <- rbmv_pal("main", plotly = T)[1]
ruby <- rbmv_pal("main", plotly = T)[2]
pale_black <- rbmv_pal("main", plotly = T)[5]
```
LA County {data-icon="fa-map"}
=====================================
Row {data-height=505}
-------------------------------------
###
```{r}
bins <- c(0, 25, 50, 75, 100, 150, 200, 250, 300, 350, 400, 450, Inf)
pal <- colorBin(
palette = rbmv_pal("spectrum", plotly = T),
domain = lac_covid19$total_cases,
bins = bins)
label <- str_glue("{lac_covid19$label}
Total cases: {lac_covid19$total_cases}") %>%
lapply(htmltools::HTML)
lac_covid19 %>%
leaflet() %>%
addTiles(mapbox, attribution = paste("Data from Los Angeles County Department of Publich Health COVID-19 location table")) %>%
addPolygons(
fillColor = ~pal(total_cases),
weight = 1.5,
fillOpacity = 0.7,
smoothFactor = 0.5,
color = "white",
label = label,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addLegend(
pal = pal, values = ~total_cases, opacity = 0.7,
title = NULL, position = "bottomright") %>%
setView(-118.2, 34, zoom = 9.5)
```
Row {data-height=320}
-------------------------------------
###
```{r}
source <- list(
x = 1, y = -0.24, text = "Source: 2019 Novel Coronavirus COVID-19 (2019-nCoV)\nData Repository by Johns Hopkins CSSE",
showarrow = F, xref = "paper", yref = "paper", xanchor = "right", yanchor = "auto",
xshif = 0, yshift = 0, font = list(size = 9, color = "grey")
)
covid19_cases_top10_us %>%
plot_ly(x = ~date, y = ~cases, color = ~location, line = list(color = rbmv_pal(plotly = T)[3], width = .75),
type = "scatter", mode = "lines", hoverinfo = "text",
text = ~paste(" Location: ", location,
" Case type: ", case_type,
" Cases: ", scales::comma(cases))) %>%
add_trace(x = ~date, y = ~cases, color = ~county, line = list(color = rbmv_pal(plotly = T)[2], width = 2),
data = covid19_time_series_cases, hoverinfo = "text",
text = ~paste(" Location: ", county,
" Case type: ", case_type,
" Cases: ", scales::comma(cases))) %>%
layout(title = " ",
xaxis = list(title = " "),
yaxis = list(title = "Log(Cases)\n", type = "log"),
margin = list(b = 50),
legend = list(
title = list(text = " How LA County's COVID-19 Cases Compare Across the US "),
x = 100, y = .99, font = list(size = 9)),
annotations = source) %>%
config(displayModeBar = FALSE)
```
Row {data-height=175}
-------------------------------------
###
```{r}
valueBox(page_updated_string, color = light_blue, icon = "fa-calendar", caption = "Data pulled from both LAC DPH and John Hopkins CSSE")
```
###
```{r}
total_cases <- scales::comma(last(covid19_time_series_cases$cases))
valueBox(total_cases, color = ruby, icon = "fa-ambulance", caption = "Total Lab Confirmed COVID-19 Cases in LAC")
```
###
```{r}
total_deaths <- scales::comma(last(covid19_time_series_deaths$cases))
valueBox(total_deaths, color = pale_black, icon = "fa-medkit", caption = "Total COVID-19 Related Deaths")
```
Global {data-icon="fa-globe"}
=====================================
###
```{r}
bins <- c(0, 3000, 6000, 9000, 12000, 30000, 60000, 90000, 120000, Inf)
pal <- colorBin(
palette = rbmv_pal("warm", plotly = T),
domain = world$Confirmed,
bins = bins)
label <- str_glue("Location: {world$Combined_Key}
Cases: {scales::comma(world$Confirmed)}
Deaths: {scales::comma(world$Deaths)}
Active: {scales::comma(world$Active)}
Recovered: {scales::comma(world$Recovered)}
Last Update: {world$Last_Update}") %>%
lapply(htmltools::HTML)
world %>%
leaflet() %>%
addTiles(mapbox, attribution = paste("Data from Novel Coronavirus (COVID-19) Cases, provided by JHU CSSE")) %>%
addCircleMarkers(
lng = ~long, lat = ~lat,
label = ~label,
color = ~pal(Confirmed),
radius = ~ifelse(Confirmed >= 30000, 25, 15),
stroke = FALSE, fillOpacity = .7,
clusterOptions = markerClusterOptions(
showCoverageOnHover = FALSE),
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addLegend(title = "COVID-19 Cases", opacity = 0.6, pal = pal, values = ~Confirmed, position = "bottomright")
```